﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VK.Casemeet.Lines;
using VK.Comm;
using System.Web.Script.Serialization;

namespace VK.Casemeet.Web.Admin.Lines
{
    public partial class LineAdd : System.Web.UI.Page
    {
        public int destinationArea1, destinationArea2, destinationArea3,
            stagingArea1, stagingArea2, stagingArea3;
        public string lineFeatures, referenceStroke, costDescription, precautions;
        public string imgUrl = "Null.jpg";

        protected void Page_Load(object sender, EventArgs e)
        {
            (this.Master as Site).SiteMapPathControl.MapPathList = new List<MapPath>(){
                new MapPath{ Text="桌面",Url="/Admin/Home.aspx"},
                new MapPath{ Text="活动线路列表",Url="Index.aspx"},
                new MapPath{ Text="新增活动线路",Url="#"}
            };

            if (!IsPostBack)
            {
                LineHelper.BindTypeDDL(ddlType, OptionType.Select);
                LineHelper.BindHolidayDDL(ddlHoliday, OptionType.Select);
                LineHelper.BindStayDDL(ddlStay, OptionType.Select);
                LineHelper.BindGradeDDL(ddlGrade, OptionType.Select);

                txtCreated.Text = DateTime.Now.ToString();
            }
        }

        protected void butSave_Click(object sender, EventArgs e)
        {
            LineEntity line = LoadEntity();

            if (LineBLL.InsertLine(line) > 0)
            {
                Message.Msg(this.Page, CS.MsgAddSuccess, MessageType.Success);
                Util.ClearControl((this.Master as Site).contentBody.Controls);
                txtCreated.Text = DateTime.Now.ToString();
                txtHits.Text = "0";
            }
            else
            {
                Message.Msg(this.Page, CS.MsgAddFailure, MessageType.Failure);
            }
        }

        protected LineEntity LoadEntity()
        {
            LineEntity line = new LineEntity();
            line.ShopID = int.Parse(txtShopID.Text);
            line.Name = txtName.Text;
            line.DestinationProvinceID = Util.GetFormInt("ddlDestinationArea1");
            line.DestinationCityID = Util.GetFormInt("ddlDestinationArea2");
            line.DestinationAreaID = Util.GetFormInt("ddlDestinationArea3");
            line.StagingProvinceID = Util.GetFormInt("ddlStagingArea1");
            line.StagingCityID = Util.GetFormInt("ddlStagingArea2");
            line.StagingAreaID = Util.GetFormInt("ddlStagingArea3");
            line.Type = int.Parse(ddlType.SelectedValue);
            line.Days = int.Parse(txtDays.Text);
            line.Holiday = int.Parse(ddlHoliday.SelectedValue);
            line.Stay = int.Parse(ddlStay.SelectedValue);
            line.Grade = int.Parse(ddlGrade.SelectedValue);
            line.Price = Util.GetTextDecimal(txtPrice.Text);
            line.LineFeatures = Util.GetFormString("txtLineFeatures");
            line.ReferenceStroke = Util.GetFormString("txtReferenceStroke");
            line.CostDescription = Util.GetFormString("txtCostDescription");
            line.Precautions = Util.GetFormString("txtPrecautions");
            line.Hits = int.Parse(txtHits.Text);
            line.Created = Util.ConverDateTime(txtCreated.Text);
            line.IsEnable = chkIsEnable.Checked;
            string[] imgUrlArray = new JavaScriptSerializer().Deserialize<string[]>(Util.GetFormString("hidImgUrlArray"));
            line.ImgUrllst = new List<string>(imgUrlArray);
            ScheduleEntity[] scheduleArray = new JavaScriptSerializer().Deserialize<ScheduleEntity[]>(Util.GetFormString("hidScheduleArray"));
            line.Schedulelst = new List<ScheduleEntity>(scheduleArray);

            return line;
        }
    }
}